/*
 * httpresponse.cpp
 *
 *  Created on: Apr 14, 2009
 *      Author: kuncai
 */

#include "httpresponse.h"
HttpResponse::HttpResponse():HttpMsg(){
	sStatus = "";
	sVersion = "HTTP/1.1";
	sReason = "";
}
HttpResponse::HttpResponse(string msg):HttpMsg(msg){
	sStatus = "";
	sVersion = "HTTP/1.1";
	sReason = "";
	Formate(msg);
}
HttpResponse::~HttpResponse(){}
int HttpResponse::SetStatus(string status){
	sStatus = status;
	return 1;
}
string HttpResponse::GetStatus(){
	return sStatus;
}
int HttpResponse::SetVersion(string version){
	sVersion = version;
	return 1;
}
string HttpResponse::GetVersion(){
	return sVersion;
}
int HttpResponse::SetReason(string reason){
	sReason = reason;
	return 1;
}
string HttpResponse::GetReason(){
	return sReason; 
}
Buffer HttpResponse::ToBuffer(){
	Buffer bMsg;
	if(sMsg != ""){
		bMsg.Append(sMsg.c_str(),sMsg.length());
		bMsg.Append(sContent.GetByteBuffer(),sContent.GetLimit());
		return bMsg;
	}
	//response-line
	sMsg += GetVersion();
	sMsg += " "+GetStatus();
	sMsg += " "+GetReason();
	sMsg += "\r\n";
	//header
	map<string,string>::iterator iter;
	string name,value;
	for(iter = mHeaderList.begin();iter != mHeaderList.end(); iter++){
		name = value = "";
		name = iter->first;
		value = iter->second;
		if(name != ""){
			sMsg += name + ": " + value + "\r\n";
		}
	}	
	sMsg += "\r\n";
	bMsg.Append(sMsg.c_str(),sMsg.length());
	bMsg.Append(sContent.GetByteBuffer(),sContent.GetLimit());
	return bMsg;
}
void HttpResponse::Formate(string str){
}
